package com.management.demo.valid.annotation;

import com.management.demo.common.enums.SexEnum;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.ArrayList;
import java.util.List;

// 自定义性别校验注解
public class SexValidator implements ConstraintValidator<Sex, Integer> {

    private final List<Integer> sexList = new ArrayList<>();

    @Override
    public void initialize(Sex annotation) {
        for (SexEnum value : SexEnum.values()) {
            Integer id = value.getId();
            sexList.add(id);
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return sexList.contains(value);
    }
}
